package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
     //登录
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //根据页面提交的username查询数据库
        lambdaQueryWrapper.eq(Employee::getUsername,employee.getUsername());
        //数据库的one
        Employee one = employeeService.getOne(lambdaQueryWrapper);
        //页面输入的password
        String password = employee.getPassword();
        //进行加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(one ==null){
            return R.error("账号登陆失败！！！");
        }

        if(!password.equals(one.getPassword())){
            return R.error("登陆失败！！！");
        }
        if(one.getStatus()==0){
            return R.error("该员工账号被禁用");
        }
        request.getSession().setAttribute("employee",one.getId());
        return R.success(one);
    }
       //退出
       @PostMapping("/logout")
       public R<String> logout(HttpServletRequest request){
         request.getSession().removeAttribute("employee");
         return R.success("退出成功");
       }

//       分页查询+条件查询
      @GetMapping("/page")
      public R<Page> page(Integer page,Integer pageSize,String name){

          Page employeePage = new Page(page,pageSize);

          LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

          wrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

          employeeService.page(employeePage, wrapper);

          return R.success(employeePage);

      }

      //新增员工
      @PostMapping
      public  R<String> add(@RequestBody Employee employee){
          employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//          employee.setCreateTime(LocalDateTime.now());
//          employee.setUpdateTime(LocalDateTime.now());
//          Long ById = (Long) request.getSession().getAttribute("employee");
//          employee.setCreateUser(ById);
//          employee.setUpdateUser(ById);
          employeeService.save(employee);
          return R.success("新增员工成功！！");
      }
      //修改id查询
    @GetMapping("/{id}")
      public R<Employee> updateById(@PathVariable Long id){
        Employee byId = employeeService.getById(id);
        if(byId!=null){
            return R.success(byId);
        }
        return R.error("无相关人员信息");
    }
    //全部修改查询
    @PutMapping
     public R<String> update(@RequestBody Employee employee){
//       Long longId = (Long) request.getSession().getAttribute("employee");
//       employee.setUpdateTime(LocalDateTime.now());
//       employee.setUpdateUser(longId);
       employeeService.updateById(employee);
        return R.success("修改成功！！！");
    }



}
